package netWorkPrograming.tcp.tcp03;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/5/27
 * 需求3：服务端接收多条消息，服务端也可以发送多条消息；
 * 客户端可以发送多条消息，也可以接收多条消息；（一对一，阻塞的现象）
 */
public class ServerDemo {
    public static void main(String[] args) {
        System.out.println("------------------服务端--------------");
        try(ServerSocket ss=new ServerSocket(8888);
            Socket socket=ss.accept();
            PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
            BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            Scanner sc=new Scanner(System.in);){
            System.out.println("客户端已成功接入！");
            //服务端读
            String s=null;
            while((s=in.readLine())!=null){
                if("exit".equals(s)){
                    System.out.println("客户端请求退出！");
                    break;
                }
                System.out.println(socket.getRemoteSocketAddress()+"\t"+s);
                //服务端写
                System.out.println("请说：");
                String str=sc.nextLine();
                out.println(str);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
